<?php

namespace app\agent\controller;

use app\common\Common;
use app\common\BaseHelper;

use think\Exception;
use think\Request;

/**
 * 代理政策
 */
class Agentpolicy extends Base
{
    public function _initialize()
    {

        $this->exceptAction = ['ajaxAgentpolicyList'];
        parent::_initialize();
    }

    public function index(){

        return $this->view->fetch();
    }

    public function ajaxAgentpolicyList(){
        $request = $this->request;
        $param = $request->param();
        $model = model($this->model);

        // 每页起始条数
        $start = $param['start'];
        // 每页显示条数
        $length = $param['length'];
        // 排序条件
        $columns = $param['order'][0]['column'];
        $order = $param['columns'][$columns]['data'].' '.$param['order'][0]['dir'];

        $where = [];
        if($param['search']['value']) {
            $filter = json_decode($param['search']['value'],true);
            if($filter['levelName']){
                $where['levelName'] = ['like', '%'.$filter['levelName'].'%'];
            }
        }

        $list = $model->where($where)->limit($start, $length)->order($order)->select();
        $count = $model->where($where)->count();

        $result = [
            'status' => '1',
            'draw' => $param['draw'],
            'data' => $list,
            'recordsFiltered' => $count,
            'recordsTotal' => $count,
        ];

        return json($result);
    }

    public function add(){

        return $this->view->fetch();
    }

    public function addPost(){
        $request = $this->request;
        if($this->request->isAjax()) {
            $model = model($this->model);
            $data = $this->request->param();
            // Insert data
            $data['id'] = BaseHelper::getUUID();
            $data['createDate'] = time();
            $data['createOper'] = $this->admin->id;
            $data['reserveAmount'] = $data['reserveAmount'] ? $data['reserveAmount'] * 100 : '';
            $data['buyingPrice'] = $data['buyingPrice'] ? $data['buyingPrice'] * 100 : '';

            try {
                $result = $model->save($data);

                if($result !== false) {
                    // 写入日志
                    $logInfo = $this->admin->loginName . '添加了一条' . $this->model . '数据。';
                    common::agentLog($request, $logInfo);

                    return json(['status' => 1, 'msg' => '添加成功！']);
                } else {
                    return json(['status' => 0, 'msg' => '添加失败！']);
                }
            } catch (Exception $e) {
                return json(['status' => 0, 'msg' => $e->getMessage()]);
            }
        } else {
            return json(['status' => 0, 'msg' => '请求方式不正确！']);
        }
    }

    public function edit(){
        $model = model($this->model);
        $id = $this->request->param('id');
        $where = [
            'a.id' => $id,
        ];
        $info = $model->alias('a')->where($where)->find();

        if(!$info){
            return $this->error('error !', 'agent/agentpolicy/index');
        }
        // 代理级别
        $this->assign('info', $info);

        return $this->view->fetch();
    }

    public function editPost(){
        $request = $this->request;
        if($this->request->isAjax()) {
            $model = model($this->model);
            $data = $this->request->param();
            // Update data
            $data['updateDate'] = time();
            $data['updateOper'] = $this->admin->id;
            $data['reserveAmount'] = $data['reserveAmount'] ? $data['reserveAmount'] * 100 : '';
            $data['buyingPrice'] = $data['buyingPrice'] ? $data['buyingPrice'] * 100 : '';

            try {
                $result = $model->allowField(true)->save($data, ['id' => $data['id']]);

                if($result !== false) {
                    // 写入日志
                    $logInfo = $this->admin->loginName . '更新了一条' . $this->model . '数据。';
                    common::agentLog($request, $logInfo);

                    return json(['status' => 1, 'msg' => '保存成功！']);
                } else {
                    return json(['status' => 0, 'msg' => '保存失败！']);
                }
            } catch (Exception $e) {
                return json(['status' => 0, 'msg' => $e->getMessage()]);
            }
        } else {
            return json(['status' => 0, 'msg' => '请求方式不正确！']);
        }
    }

    public function delete(){
        $request = $this->request;
        if($this->request->isAjax()) {
            $model = model($this->model);
            $id = $this->request->param('id');

            try {
                $info = $model->find(['id', $id]);
                if(!$info){
                    return json(['status' => 0, 'msg' => '该数据不存在！']);
                }
                $result = $model->where('id', $id)->delete();

                if($result !== false) {
                    // 写入日志
                    $logInfo = $this->admin->loginName . '删除了1条' . $this->model . '数据。';
                    common::agentLog($request, $logInfo);

                    return json(['status' => 1, 'msg' => '删除成功！']);
                } else {
                    return json(['status' => 0, 'msg' => '删除失败！']);
                }
            } catch (Exception $e) {
                return json(['status' => 0, 'msg' => $e->getMessage()]);
            }
        } else {
            return json(['status' => 0, 'msg' => '请求方式不正确！']);
        }
    }

}